package com.leil.nio.channel;

import javax.imageio.IIOException;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class FileChannelOutputDemo {

    public static void main(String[] args) {
        FileOutputStream outputStream = null;
        FileChannel channel = null;
        try {
            outputStream = new FileOutputStream("d:/file-channel.txt");
            channel = outputStream.getChannel();

            ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
            byteBuffer.put("Hello Netty".getBytes());
            byteBuffer.flip();

            channel.write(byteBuffer);

            channel.close();
            outputStream.flush();
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                outputStream.flush();
                outputStream.close();
                channel.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }

}
